from flask import Flask, request, jsonify
import jieba
import re

app = Flask(__name__)

def preprocess(text):
    # 清理非中文、数字、字母的字符
    # cleaned = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9]', '', text)
    # 分词并转换为集合
    words = set(jieba.lcut(text))
    # 如果有中文，需要分成单字
    zh_char_set = set()
    zh_word_set = set()
    for word in words:
        for char in word:
            if '\u4e00' <= char <= '\u9fff':
                zh_char_set.add(char)
                zh_word_set.add(word)
    if zh_char_set:
        words.update(zh_char_set)
    for word in zh_word_set:
        words.remove(word)
    return words

def jaccard_similarity(str1, str2):
    set1 = preprocess(str1)
    set2 = preprocess(str2)
    print(set1)
    print(set2)

    if not set1 and not set2:
        return 1.0  # 两个空字符串视为完全相似
    intersection = set1 & set2
    union = set1 | set2
    return len(intersection) / len(union)

@app.route('/compare')
def compare():
    data = request.args
    s1 = data.get('str1')
    s2 = data.get('str2')
    similarity = jaccard_similarity(s1, s2)
    return jsonify({"similarity": round(similarity, 4)})

if __name__ == '__main__':
    # 可选：加载自定义词典（提高分词准确性）
    # jieba.load_userdict('my_dict.txt')
    app.run(host='0.0.0.0', port=5001, debug=True)